{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6030245d",
   "metadata": {},
   "source": [
    "# Python 缩进与注释语法详解\n",
    "\n",
    "## 1. 缩进规则\n",
    "### 1.1 基本规则\n",
    "- Python使用缩进来定义代码块\n",
    "- 标准缩进为4个空格\n",
    "- 禁止混用空格和Tab\n",
    "- 同一代码块必须保持相同缩进\n",
    "\n",
    "### 1.2 正确示例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4bf21ef4",
   "metadata": {},
   "outputs": [],
   "source": [
    "def calculate_sum(numbers):\n",
    "    total = 0\n",
    "    for num in numbers:\n",
    "        if num > 0:  # 嵌套代码块\n",
    "            total += num\n",
    "    return total"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82637fd2",
   "metadata": {},
   "source": [
    "###  1.3错误示例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "1b07a94d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 错误1：混用空格和Tab\n",
    "def bad_indent():\n",
    "    print(\"Spaces\")  # 4个空格\n",
    "    print(\"Tab\")     # 1个Tab"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "5119121c",
   "metadata": {},
   "outputs": [
    {
     "ename": "IndentationError",
     "evalue": "unindent does not match any outer indentation level (<tokenize>, line 4)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m<tokenize>:4\u001b[1;36m\u001b[0m\n\u001b[1;33m    print(\"Level 2\")  # 缩进不一致\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m unindent does not match any outer indentation level\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# 错误2：缩进不一致\n",
    "def inconsistent():\n",
    "    print(\"Level 1\")\n",
    "  print(\"Level 2\")  # 缩进不一致"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b83a0fa8",
   "metadata": {},
   "source": [
    "# 2. 注释语法\n",
    "## 2.1 单行注释\n",
    "1. 使用 # 开头\n",
    "2. 注释内容与 # 之间保留一个空格\n",
    "3. 行尾注释与代码间隔至少两个空格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "828d4399",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 正确示例\n",
    "x = 5  # 初始化变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "1803844c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 错误示例\n",
    "x=5#缺少空格"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "87c41aa8",
   "metadata": {},
   "source": [
    "## 2.2 多行注释\n",
    "1. 使用三个连续引号 \"\"\" 或 '''\n",
    "\n",
    "2. 适用于函数/类文档字符串\n",
    "\n",
    "3. 也可用于临时注释大段代码"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
